/**
 * Copyright (c) 2009-2012 XTEAM
 * All rights reserved.
 */
/**
 * File：FileController.java
 * History:
 *         2012-5-17: Initially created, Chrise.
 */
package com.xteam.ecsystem.contorller;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.tangkf.utils.bean.ReqHelper;
import com.xteam.ecsystem.base.FileObject;
import com.xteam.ecsystem.service.admin.FileService;
import com.xteam.ecsystem.service.admin.SystemLogService;
import com.xteam.ecsystem.utils.ConstantLocation;
import com.xteam.ecsystem.utils.PageCenterStation;

/**
 * 文件管理控制层。
 * @author Chrise
 */
@Controller
public class FileController {
	
	@Autowired
	private FileService fileService;
	
	@Autowired
	private SystemLogService logService;
	
	/**
	 * 获取资源目录。
	 * @author Chrise 2012-5-21
	 * @param request
	 * @param response
	 * @throws IOException
	 */
	@RequestMapping("/getResDirectorys.do")
	public void getResDirectorys(HttpServletRequest request, HttpServletResponse response) throws IOException {
		this.getDirectorys(true, request, response);
	}
	
	/**
	 * 获取模板目录。
	 * @author Chrise 2012-6-1
	 * @param request
	 * @param response
	 * @throws IOException
	 */
	@RequestMapping("/getTplDirectorys.do")
	public void getTplDirectorys(HttpServletRequest request, HttpServletResponse response) throws IOException {
		this.getDirectorys(false, request, response);
	}
	
	/**
	 * 获取资源文件。
	 * @author Chrise 2012-5-23
	 * @param request
	 * @param response
	 * @throws IOException
	 */
	@RequestMapping("/getResFiles.do")
	public void getResFiles(HttpServletRequest request, HttpServletResponse response) throws IOException {
		this.getFiles(true, request, response);
	}
	
	/**
	 * 获取模板文件。
	 * @author Chrise 2012-6-1
	 * @param request
	 * @param response
	 * @throws IOException
	 */
	@RequestMapping("/getTplFiles.do")
	public void getTplFiles(HttpServletRequest request, HttpServletResponse response) throws IOException {
		this.getFiles(false, request, response);
	}
	
	/**
	 * 编辑名称。
	 * @author Chrise 2012-5-24
	 * @param request
	 * @param response
	 * @throws IOException
	 */
	@RequestMapping("/editName.do")
	public void editName(HttpServletRequest request, HttpServletResponse response) throws IOException {
		String data = ReqHelper.getRequestString(request, "data");
		String lslUser = ReqHelper.getRequestString(request, "lslUser");
		String lslAddress = ReqHelper.getRequestString(request, "lslAddress");
		String lslFunction = ConstantLocation.LOG_WEBRES_MGR;
		String lslType = ConstantLocation.LOG_CREATEFOLDER;
		
		JSONObject object = JSONObject.fromObject(data);
		JSONObject result = new JSONObject();
		
		if (this.fileService.editFileName(object.getBoolean("forres"), object.getBoolean("iscreate"), 
			object.getString("name"), object.getString("orgin"), object.getString("parent"))) {
			result.put("issuccess", true);
		} else {
			result.put("issuccess", false);
			result.put("message", "重复的文件名！");
		}
		
		if (object.getBoolean("forres")) {
			if (!object.getBoolean("iscreate")) lslType = ConstantLocation.LOG_RENAMERESOURCE;
		}else{
			lslFunction = ConstantLocation.LOG_WEBTPL_MGR;
			if (!object.getBoolean("iscreate")) lslType = ConstantLocation.LOG_RENAMERESOURCE;
		}
		
		this.logService.saveSystemLogs(lslUser, lslAddress, lslFunction, "editFileName", lslType, null);
		
		response.getWriter().write(result.toString());
	}
	
	/**
	 * 删除资源文件。
	 * @author Chrise 2012-5-25
	 * @param request
	 * @param response
	 * @throws IOException
	 */
	@RequestMapping("/delResFiles.do")
	public void delResFiles(HttpServletRequest request, HttpServletResponse response) throws IOException {
		this.delFiles(true, request, response);
	}
	
	/**
	 * 删除模板文件。
	 * @author Chrise 2012-6-1
	 * @param request
	 * @param response
	 * @throws IOException
	 */
	@RequestMapping("/delTplFiles.do")
	public void delTplFiles(HttpServletRequest request, HttpServletResponse response) throws IOException {
		this.delFiles(false, request, response);
	}
	
	/**
	 * 上穿文件。
	 * @author Chrise 2012-5-29
	 * @param request
	 * @param response
	 * @throws Exception
	 */
	@SuppressWarnings("unchecked")
	@RequestMapping("/uploadFiles.do")
	public void uploadFiles(HttpServletRequest request, HttpServletResponse response) throws Exception {
		String data = ReqHelper.getRequestString(request, "data");
		String lslUser = ReqHelper.getRequestString(request, "lslUser");
		String lslAddress = ReqHelper.getRequestString(request, "lslAddress");
		String lslFunction = ConstantLocation.LOG_WEBRES_MGR;
		
		JSONObject object = JSONObject.fromObject(data);
		List list = new ServletFileUpload(new DiskFileItemFactory()).parseRequest(request);
		
		this.fileService.uploadFiles(object.getBoolean("forres"), object.getString("parent"), list);
		
		if (!object.getBoolean("forres")) lslFunction = ConstantLocation.LOG_WEBTPL_MGR;
		
		this.logService.saveSystemLogs(lslUser, lslAddress, lslFunction, "uploadFiles", 
				ConstantLocation.LOG_UPLOADFILE, null);
	}
	
	/**
	 * 编辑内容。
	 * @author Chrise 2012-5-30
	 * @param request
	 * @param response
	 * @throws IOException
	 */
	@RequestMapping("/editContent.do")
	public void editContent(HttpServletRequest request, HttpServletResponse response) throws IOException {
		String data = ReqHelper.getRequestString(request, "data");
		String lslUser = ReqHelper.getRequestString(request, "lslUser");
		String lslAddress = ReqHelper.getRequestString(request, "lslAddress");
		String lslFunction = ConstantLocation.LOG_WEBRES_MGR;
		String lslType = ConstantLocation.LOG_CREATEFILE;
		
		JSONObject object = JSONObject.fromObject(data);		
		JSONObject result = new JSONObject();
		
		if (this.fileService.editContent(object.getBoolean("forres"), object.getBoolean("iscreate"), 
			object.getString("name"), object.getString("parent"), object.getString("content"))) {
			result.put("issuccess", true);
			result.put("message", "保存成功！");
		} else {
			result.put("issuccess", false);
			result.put("message", "已存在同名的目录或文件！");
		}
		
		if (object.getBoolean("forres")) {
			if (!object.getBoolean("iscreate")) lslType = ConstantLocation.LOG_MODIFY;
		}else{
			lslFunction = ConstantLocation.LOG_WEBTPL_MGR;
			if (!object.getBoolean("iscreate")) lslType = ConstantLocation.LOG_MODIFY;
		}
		
		this.logService.saveSystemLogs(lslUser, lslAddress, lslFunction, "editContent", lslType, null);
		
		response.getWriter().write(result.toString());
	}
	
	/**
	 * 获取内容。
	 * @author Chrise 2012-5-30
	 * @param request
	 * @param response
	 * @throws IOException
	 */
	@RequestMapping("/getContent.do")
	public void getContent(HttpServletRequest request, HttpServletResponse response) throws IOException {
		String data = ReqHelper.getRequestString(request, "data");
		JSONObject object = JSONObject.fromObject(data);
		
		String content = this.fileService.getContent(object.getBoolean("forres"), 
			object.getString("name"), object.getString("parent"));
		
		JSONObject result = new JSONObject();
		if (content != null) {
			result.put("issuccess", true);
			result.put("message", content);
		} else {
			result.put("issuccess", false);
			result.put("message", "指定的文件不存在！");
		}
		
		response.getWriter().write(result.toString());
	}
	
	/**
	 * 获取模板。
	 * @author Chrise 2012-6-11
	 * @param request
	 * @param response
	 * @throws IOException
	 */
	@RequestMapping("/getTemplates.do")
	public void getTemplates(HttpServletRequest request, HttpServletResponse response) throws IOException {
		List<FileObject> list = this.fileService.getTemplates();
		JSONArray result = JSONArray.fromObject(list);
		response.getWriter().write(result.toString());
	}
	
	/**
	 * 获取模板方案。
	 * @author Chrise 2012-6-12
	 * @param request
	 * @param response
	 * @throws IOException
	 */
	@RequestMapping("/getTemplateNames.do")
	public void getTemplateNames(HttpServletRequest request, HttpServletResponse response) throws IOException {
		List<FileObject> list = this.fileService.getTemplateNames();
		JSONArray result = JSONArray.fromObject(list);
		response.getWriter().write(result.toString());
	}
	
	/**
	 * 获取目录。
	 * @author Chrise 2012-5-28
	 * @param forRes
	 * @param request
	 * @param response
	 * @throws IOException
	 */
	private void getDirectorys(boolean forRes, HttpServletRequest request, HttpServletResponse response) throws IOException {
		List<FileObject> list = this.fileService.getDirectorys(forRes);
		JSONArray result = JSONArray.fromObject(list);
		response.getWriter().write(result.toString());
	}
	
	/**
	 * 获取文件。
	 * @author Chrise 2012-5-28
	 * @param forRes
	 * @param request
	 * @param response
	 * @throws IOException
	 */
	private void getFiles(boolean forRes, HttpServletRequest request, HttpServletResponse response) throws IOException {
		String parentPath = ReqHelper.getRequestString(request, "parentPath");
		int pageIndex = ReqHelper.getRequestInt(request, "pageIndex");
		int pageSize = ReqHelper.getRequestInt(request, "pageSize");
		
		PageCenterStation<FileObject> pcs = this.fileService.getFilesByPath(forRes, parentPath, pageIndex, pageSize);
		JSONObject result = JSONObject.fromObject(pcs);
		response.getWriter().write(result.toString());
	}
	
	/**
	 * 删除文件。
	 * @author Chrise 2012-5-28
	 * @param forRes
	 * @param request
	 * @param response
	 * @throws IOException
	 */
	private void delFiles(boolean forRes, HttpServletRequest request, HttpServletResponse response) throws IOException {
		String data = ReqHelper.getRequestString(request, "data");
		String lslUser = ReqHelper.getRequestString(request, "lslUser");
		String lslAddress = ReqHelper.getRequestString(request, "lslAddress");
		String lslFunction = ConstantLocation.LOG_WEBRES_MGR;
		String lslType = ConstantLocation.LOG_DELETE_FILE;
		
		JSONArray array = JSONArray.fromObject(data);
		
		List<String> paths = new ArrayList<String>();
		for (int index = 0; index < array.size(); index ++) {
			JSONObject object = array.getJSONObject(index);
			paths.add(object.getString("path"));
		}
		
		JSONObject result = new JSONObject();
		String name = this.fileService.delFilesByPaths(forRes, paths);
		if (name == null) {
			result.put("issuccess", true);
		} else {
			result.put("issuccess", false);
			result.put("message", name + " 删除失败！");
		}
		
		if (!forRes) {
			lslFunction = ConstantLocation.LOG_WEBTPL_MGR;
			lslType = ConstantLocation.LOG_DELETE_TPL;
		}
		
		this.logService.saveSystemLogs(lslUser, lslAddress, lslFunction, "delFilesByPaths", lslType, null);
		
		response.getWriter().write(result.toString());
	}
}
